//
//  SuggestionViewController.m
//  DTSampleSocket
//
//  Created by ZhangYeKun on 2017/3/8.
//  Copyright © 2017年 Fidetro. All rights reserved.
//

#import "SuggestionViewController.h"
#import "FIDPlaceholderTextView.h"
//#import "PublicFeedbackApi.h"
@interface SuggestionViewController ()
@property (weak, nonatomic) IBOutlet UITextField *contactTextField;
@property (weak, nonatomic) IBOutlet FIDPlaceholderTextView *suggestTextView;
@property (weak, nonatomic) IBOutlet UILabel *wordNumLabel;

@end

@implementation SuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.suggestTextView addObserver:self forKeyPath:@"textLength" options:NSKeyValueObservingOptionNew||NSKeyValueChangeOldKey context:nil];
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setNavigationTitle:@"意见反馈" titleColor:[UIColor whiteColor]];
    [self setRightButtonTitle:@"提交"];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F9"];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    
}



- (void)onRightButtonClick:(id)sender{
//    NSString *contact = self.contactTextField.text;
    NSString *suggest = self.suggestTextView.text;
    if ([suggest isEqualToString:@"写下问题和建议，我们将为您不断改进..."]) {
        [SVProgressHUD showErrorWithStatus:@"建议不能为空噢"];
        return;
    }
    
    if (![suggest isChineseCharacter]) {
        [SVProgressHUD showErrorWithStatus:@"建议只能输入中文、字母或数字"];
        return;
    }

    if (suggest.length == 0 ) {
        
        [SVProgressHUD showInfoWithStatus:@"反馈内容不能为空" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    if ([NSString isContainsTwoEmoji:suggest] ) {
        [SVProgressHUD showInfoWithStatus:@"意见不能使用表情"];
        return;
    }
    
    NSString *feedback = [suggest stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    FeedBackApi *api = [[FeedBackApi alloc]initWith:@{@"content":feedback,@"type_id":@"1121"}];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dic = [api responseJSONObject];
        if ([dic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showInfoWithStatus:@"感谢您的反馈！"];
            self.suggestTextView.text = @"";
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showInfoWithStatus:dic[@"errmsg"]];
        }
    } failure:^(YTKBaseRequest *request) {
        [SVProgressHUD showInfoWithStatus:@"网络问题"];
    }];

    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if (change[NSKeyValueChangeNewKey] != change[NSKeyValueChangeOldKey]) {
        self.wordNumLabel.text = [NSString stringWithFormat:@"%@/400",change[NSKeyValueChangeNewKey]];
    }
}



- (void)setNavigationStyle{
    
    [self setRightButtonTitle:@"提交"];
    [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self setNavigationTitle:@"意见反馈" titleColor:[UIColor whiteColor]];
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    
}

- (void)dealloc
{
    [self.suggestTextView removeObserver:self forKeyPath:@"textLength"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
